﻿using System;
using System.Web.UI;
using AcctrueWMS.Foundation.Data;
using AcctrueWMS.Foundation.Web;
using AcctrueWMS.Foundation.Util;
using System.Reflection;
using AcctrueWMS.Web.Service;
using AcctrueWMS.Foundation.Web.Session;
using AcctrueWMS.Foundation.Web.Permission;
using AcctruePMS.Bll.Manager;
using AcctruePMS.Bll.Common;
namespace AcctrueWMS.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void imgLogin_Click(object sender, ImageClickEventArgs e)
        {
            string str_userName = this.txtLoginName.Text;
            string str_loginPws = AuthHelper.Encrpt(this.txtPassword.Text);//加密密码
            string message = string.Empty;

            string configValidate = System.Configuration.ConfigurationManager.AppSettings["loginValidate"];
            if (Convert.ToBoolean(configValidate))
            {
                //验证码验证
                if (!Validate(out message))
                {
                    AlertMessage(message);
                    return;
                }
            }
            UserManager user = new UserManager();
            SystemUser cUser = user.ValidateLoginToGetUser(str_userName, str_loginPws, out message);
            if (cUser != null)
            {
                Session[SessionConst.CurrentUser] = cUser;
                Session[SessionConst.UserNo] = cUser.UserNo;
                Session[SessionConst.UserName] = cUser.UserName;
                Session[SessionConst.UserRoles] = cUser.RoleCodes;
                Session[SessionConst.DisplayName] = cUser.DisplayName;
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                AlertMessage(message);
            }
        }


        /// <summary> 
        /// 弹出信息
        /// </summary>
        /// <param name="message"></param>
        private void AlertMessage(string message)
        {
            Response.Write("<script language='javascript'>alert('" + message + "');</script>");
        }
        /// <summary>
        /// 根据配置对验证码的控制
        /// </summary>
        private bool Validate(out string message)
        {
            string sessionValidate = Session["vcode"].ToString();
            if (AuthHelper.Encrpt(txtValidate.Text).ToString() != sessionValidate)
            {
                message = "验证码，不正确！";
                return false;
            }
            else
            {
                message = "验证码正确。";
                return true;
            }
        }

    }
}
